/*
 * @Author: your name
 * @Date: 2021-11-05 13:24:26
 * @LastEditTime: 2022-01-26 10:25:11
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \star-cat-h5\permission.js
 */
import router from './router'
import store from './vuex/store'
import {
  setToken,
  removeToken
} from "./util/auth"

import {
  Toast
} from 'vant';
import {
  getAdminInfoByCode
} from "./axios/user"

// const whiteList = ['/artist_list', '/artist_edit', '/artist'] //登录路由名单
const whiteList = ['/', '/details', '/search','/customer_details','/collection_details'] //登录路由名单

router.beforeEach((to, from, next) => {
  isQYWX(to.meta);
  setTitle(to.meta) //设置title
  let review_id = sessionStorage.getItem("review_id")
  if (review_id) {
    store.commit('mutationsReview_id', review_id)
    next()
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      // console.log('白名单')
      next()
    } else {
      // console.log('请求用户信息')
      Toast.loading({
        duration: 0,
        message: "加载中...",
        overlay: true,
      });
      let param = getCode(window.location.search);
      if (process.env.VUE_APP_ENV == 'dev') {
        Toast.clear();
        setToken('8ece84e4-d00e-4ef5-9df7-1c19f7120006')
        next();
      } else
        getAdminInfoByCode(param.code).then(
          (data) => {
            let {
              id,
              code,
              msg,
              token
            } = data;
            Toast.clear();
            if (code != 0) {
              store.commit('mutationsReview_id', id)
              setToken(token)
              next();
            } else {
              removeToken();
              Toast.fail(msg);
            }
          }).catch(err => {
          removeToken();
          // console.log('用户信息获取失败')
          // store.commit('mutationsReview_id', 10001)
          Toast.fail(err);
          // next(); // 需求登录失败也进入页面时放开
        })
    }
  }
})
router.afterEach((to, from, next) => {
  window.onload = function () {
    setIphone()
  }
  $(document).ready(function () {
    setIphone()
  })
})

function setIphone() {
  if (window.screen.availHeight >= 812 && /iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase())) {
    let app = document.getElementById("iphoneBox");
    let bottom = document.getElementById("bottom-btn_box");
    if (app && bottom) {
      app.style.height = 34 + 44 + "px";
      bottom.style.paddingBottom = 34 + "px";
    }
  } else {
    let app = document.getElementById("iphoneBox");
    app.style.height = 44 + "px";
  };
}

function setTitle({
  title
}) {
  if (title)
    document.title = title;
}

function getCode(url) {
  let param = new Object();
  let str = url.substr(1);
  let strs = str.split("&");
  for (let i = 0; i < strs.length; i++) {
    param[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
  }
  return param;
}
// 判断当前是否是企业微信环境
function isQYWX({
  webRouter
}) {
  if (process.env.VUE_APP_ENV == 'dev') return;
  let userAgent = navigator.userAgent
  if (!userAgent.includes('wxwork')) {
    window.location.href = process.env.VUE_APP_WEB_BASE_URL + webRouter
  }
}